//index.js
//获取应用实例
const app = getApp()
 
Page({
  data: {
    username: '',
    password: ''
  },
  onShow: function () {
  },

  onLoad: function (e) {

    if(!e.ac){
      app.getOpenId().then(res=>{
        app.globalData.openid=res._openid
        if (res.istz){
          wx.switchTab({
            url: '../../pages/index/index',
          })
        }
      })
    }
  },
 
 
  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
 
  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
 
  // 登录处理
  login: function () {
    let that = this;
    wx.getUserProfile({
      desc: '完善会员信息',
    }).then(res=>{
      if (this.data.username.length == 0 || this.data.password.length == 0) {
        wx.showToast({
          title: '账号或密码不能为空',
          icon: 'none',
          duration: 2000
        })
      }
      else if(this.data.username.length>4){
        wx.showToast({
          title: '昵称不得超过4',
          icon: 'none',
          duration: 2000
        })
      } 
      else {
        var{username,password}=this.data;
        wx.showLoading({
          title: '正在报名',
        })
        wx.cloud.callFunction({
          name:'login',
          data:{
            type:'xuanshou',
            userInfo:{username,password},
            userWxInfo:res.userInfo
          }
        }).then(res=>{
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showModal({
            content: res.result.message,
            showCancel: false,
            title: '提示',
            success: (result) => {
              if (result.confirm){
                if (res.result.status==1){
                  wx.switchTab({
                    url: '../index/index',
                  })
                }
              }
            }
          })
        }).catch(err=>{
          wx.hideLoading({
            success: (res) => {},
          })
          console.log(err)
          wx.showToast({
            title: '报名失败请联系管理员',
          })
        })
        
      }
    }).catch(e=>{
      wx.showModal({
        showCancel:false,
        title:'提示',
        content:'请授权微信名和昵称以后登录'
      })
    })

  },

  loginYouke:function(){
    wx.getUserProfile({
      desc: '完善会员信息',
    }).then(res=>{
      wx.showModal({
        content: '确认以游客模式进入小程序',
        showCancel: true,
        title: '游客模式',
        success: (result) => {
          if (result.confirm){
            wx.cloud.callFunction({
              name:'login',
              data:{
                type:'youke',
                userWxInfo:res.userInfo
              }
            }).then(res=>{
                wx.showModal({
                  content: res.result.message,
                  showCancel: false,
                  title: '提示',
                  success: (result) => {
                    if (result.confirm){
                      if (res.result.status==1){
                        wx.switchTab({
                          url: '../index/index',
                        })
                      }
                    }
                  }
                })
            }).catch(err=>{
              console.log(err)
              wx.showToast({
                title: '登录失败请联系管理员',
              })
            })
          }
        }
      })
    }).catch(e=>{
      wx.showModal({
        showCancel:false,
        title:'提示',
        content:'请授权微信名和昵称以后登录'
      })
    })

  },


})
 